<?php

/**

$classData = [
    'name' => 'tableModel',
    'extend' => 'DB_Model',

    'variables' => [
        'validate' => [
            'type' => 'var',
            'value' => [],
            'static' => true,
            'quotes' => false
        ],
        'data' => [
            'type' => 'protected',
            'value' => '[1,2,3,4]'
        ]
    ],

    'functions' => [
        'add' => [
            'type' => 'public',
            'arguments' => [
                '&$text = ""',
                '$value = null'
            ],
            'body' => 'return $text;',
            'static' => true
        ],
        'fooo' => [
            'type' => 'public',
            'arguments' => [
                '&$text = ""',
                '$value = null'
            ],
            'body' => '$a=$r; return $text;',
            'static' => true
        ]
    ]

];
 
 */

define('__DM_CLASSGENERATOR_HEADER', '<?php // Generated by DM_Generator_Class'."\n");

class DM_Generator_Class {

    var $templatesPath = './../configs/functions/';
    
    public $variableTypes = [
        'var',
        'public',
        'protected',
        'private'
    ];


    public $functionTypes = [
        'public',
        'protected',
        'private'
    ];    
    
    public function setTemplatesPath( $path ) {
        if (is_dir($path) ) {
            $this->templatesPath = realpath( $path );
        }
    }
    
    public function assemble() {
        
    }
    
    public function loadFunctionTemplate( $tmplName ) {
        if ( is_file($this->templatesPath.$tmplName) ) {
            $templateStr = file_get_contents( $this->templatesPath.$tmplName );
            return $templateStr;
        }
        return null;
    }
    
    public function renderTemplate( $data, $tmplName ) {
        $string = $this->loadFunctionTemplate( $tmplName );
        if ( !empty($string) ) {
            $keys = array_keys($data);
            foreach( $keys as &$key ) {
                $key = '@'.$key.'@';
            }
            return str_replace( $keys, array_values($data), $string );
        }
        return null;
    }
        
    public function buildClass( $classInfo ) {
        $phpString = __DM_CLASSGENERATOR_HEADER;
        
        $phpString .= "\n".'class ';
        
        $name = 'undefinedClass';
        if ( isset($classInfo['name']) && !empty($classInfo['name']) ) {
            $name = $classInfo['name'];
            $phpString .= $name;
        }
        
        $extend = null;
        if ( isset($classInfo['extend']) && !empty($classInfo['extend']) ) {
            $extend = $classInfo['extend'];
            $phpString .= ' extends '.$extend;
        }
        
        $implements = null;
        if ( isset($classInfo['implements']) && !empty($classInfo['implements']) ) {
            if ( is_array($classInfo['implements']) ) {
                $implements = implode(', ', $classInfo['implements']);
            } elseif ( is_string($classInfo['implements']) ) {
                $implements = $classInfo['implements'];
            }
            $phpString .= ' implements '.$implements;
        }
        
        $phpString .= '{';
        
        if ( isset($classInfo['variables']) && count($classInfo['variables']) ) {
            foreach( $classInfo['variables'] as $varName => &$varInfo ) {       
                $phpString .= "\n".$this->buildVariable( $varName, $varInfo )."\n";
            }
        }
        
        if ( isset($classInfo['functions']) && count($classInfo['functions']) ) {
            foreach( $classInfo['functions'] as $funcName => $funcInfo ) {                
                $phpString .= "\n".$this->buildFunction( $funcName, $funcInfo )."\n";
            }
        }
        
        $phpString .= '}';
        
        return $phpString;
    }
    
    
    public function buildVariable( $varName, &$varInfo ) {
        $type = 'var';
        if ( isset($varInfo['type']) && in_array($varInfo['type'], $this->variableTypes) ) {
            $type = $varInfo['type'];
        }
        
        $isStatic = false;
        if ( isset($varInfo['static']) && $varInfo['static'] ) {
            $isStatic = true;
        }
        
        $value = '';
        
        if ( isset($varInfo['value']) ) {
            $value = DM_Library::buildVariableValue( $varInfo['value'] );// (string)$varInfo['value'];
        }
        
        $useQuotes = false;
        if ( isset($varInfo['value']) && is_string($varInfo['value']) && isset($varInfo['quotes']) && $varInfo['quotes'] == true ) {
            $useQuotes = true;
        }
                
        $phpString = 
            $type.' '.($isStatic ? 'static ':'').'$'.$varName
            .
            (!empty($value)? ( ' = '.
                ($useQuotes ? '\''.addslashes($value).'\'' : $value)
            ) : '').';';
        
        return $phpString;
    }
    
    /*
    public function addExpression( &$expr, $type, $data ) {
        
    }*/
    
    
    public function buildFunction( $funcName, $funcInfo ) {
        $isStatic = false;
        if ( isset($funcInfo['static']) && $funcInfo['static'] ) {
            $isStatic = true;
        }
        
        $arguments = '';
        if ( isset($funcInfo['arguments']) ) {
            if ( is_array($funcInfo['arguments']) ) {
                $arguments = implode( ',', $funcInfo['arguments'] );
            } elseif ( is_string($funcInfo['arguments']) ) {
                $arguments = $funcInfo['arguments'];
            }
        }
        
        $type = 'public';
        if ( isset($funcInfo['type']) && in_array($funcInfo['type'], $this->functionTypes) ) {
            $type = $funcInfo['type'];
        }
        
        $body = '';
        if ( isset($funcInfo['body']) && strlen($funcInfo['body']) ) {
            $body = $funcInfo['body'];
        }
        
        $phpString = 
            $type.' '.($isStatic ? 'static ':'').'function '.$funcName.'('.$arguments.')'.
            '{'."\n".$body."\n".'}';
        
        return $phpString;
    }
    
    
}